package com.example.pixelyear.sql

import android.content.Context
import com.example.pixelyear.bean.DateBean
import com.socks.library.KLog

import java.lang.ref.WeakReference

class RecordSqlUtils(val context: Context) : BaseSqlUtils(context) {

    companion object {
        private lateinit var mInstance: WeakReference<RecordSqlUtils>

        fun getInstance(context: Context): RecordSqlUtils? {
            if (!this::mInstance.isInitialized || mInstance.get() == null)
                mInstance = WeakReference(RecordSqlUtils(context))
            return mInstance.get()
        }
    }

    private val noteUtils by lazy {
        NoteSqlUtils(context)
    }

    fun addData(bean: DateBean) {
        if (!isExists(bean)) {
            write.execSQL(
                "insert into " + Sql.recordName + " (color,year,month,day,textColor,mood) values(?,?,?,?,?,?)",
                arrayOf(
                    bean.color.toString(),
                    bean.year.toString(),
                    bean.month.toString(),
                    bean.day.toString(),
                    bean.textColor.toString(),
                    bean.mood
                )
            )
        } else {
            write.execSQL(
                "update " + Sql.recordName + " set color=? , textColor=?,mood=? where year=? and month=? and day =? ",
                arrayOf(
                    bean.color.toString(),
                    bean.textColor.toString(),
                    bean.mood,
                    bean.year.toString(),
                    bean.month.toString(),
                    bean.day.toString()
                )
            )
        }
    }

    fun getData(year: Int, month: Int): MutableList<DateBean> {
        KLog.e("yxs", "获取数据：$year===$month")
        val cursor = read.rawQuery(
            "select * from " + Sql.recordName + " where year = ? and month = ? order by day asc",
            arrayOf(year.toString(), month.toString())
        )
        val list = mutableListOf<DateBean>()
        while (cursor.moveToNext()) {
            val color = cursor.getInt(cursor.getColumnIndex("color"))
            val day = cursor.getInt(cursor.getColumnIndex("day"))
            val textColor = cursor.getInt(cursor.getColumnIndex("textColor"))
            val mood = cursor.getString(cursor.getColumnIndex("mood"))
            val bean = DateBean(month, year, day, color, textColor, mood)
            KLog.e("yxs", "查询的任务：" + bean.toString())
            bean.note =
                noteUtils.getData("$year-$month-$day")?.data?.let { if (it.length > 30) it.substring(0, 30) else it }
                    ?: ""
            list.add(bean)
        }
        cursor.close()
        return list
    }

    //获取一条数据
    fun getOneData(year: Int, month: Int, day: Int): DateBean? {
        val cursor = read.rawQuery(
            "select * from " + Sql.recordName + " where year = ? and month = ? and day=?",
            arrayOf(year.toString(), month.toString(), day.toString())
        )
        val moveToFirst = cursor.moveToFirst()
        if (moveToFirst) {
            val color = cursor.getInt(cursor.getColumnIndex("color"))
            val textColor = cursor.getInt(cursor.getColumnIndex("textColor"))
            val mood = cursor.getString(cursor.getColumnIndex("mood"))
            val bean = DateBean(month, year, day, color, textColor, mood)
            KLog.e("yxs", "查询的任务：" + bean.toString())
            cursor.close()
            return bean
        }
        cursor.close()
        return null
    }

    private fun isExists(dateBean: DateBean): Boolean {
        val cursor = read.rawQuery(
            "select * from " + Sql.recordName + " where year = ? and month = ? and day = ?",
            arrayOf(dateBean.year.toString() + "", dateBean.month.toString() + "", dateBean.day.toString() + "")
        )
        val moveToFirst = cursor.moveToFirst()
        cursor.close()
        return moveToFirst
    }


    //删除一条数据
    fun delete(year: Int, month: Int, day: Int) {
        read.execSQL(
            "delete from " + Sql.recordName + " where year = ? and month = ? and day = ?",
            arrayOf("$year", "$month", "$day")
        )
    }

}
